package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.web.admin.service.FileService;
import io.minio.*;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.lang.management.MonitorInfo;
import java.util.Date;
import java.util.UUID;

@Service
@EnableConfigurationProperties(MinioProperties.class)
public class FileServiceImpl implements FileService {
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String uploadFile(MultipartFile file) {
        //上传图片到Minio的代码
        try {
            // 创建客户端
//            MinioClient minioClient =
//                    MinioClient.builder()
                            //服务器地址
//                            .endpoint(minioProperties.getEndpoint())
                            //
//                            .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
//                            .build();

            // 判断桶是否存在
            boolean found;
            found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
            if (!found) {
                //如果不存在,创建新桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            } else {
                System.out.println("Bucket 'lease' already exists.");
            }

            //上传图片
            //1获取源文件的名字 a.jpg
            String originalFilename = file.getOriginalFilename();
            //2生成UUID 作为文件名字的前缀
            String uuid = UUID.randomUUID().toString().replaceAll("_","");
            //3获取当前日期,进行指定格式的格式化
            String format = DateFormatUtils.format(new Date(),"yyy/MM/dd");
            //4拼接生成唯一文件名
            String newFileName = format+"/"+ uuid+"_"+originalFilename;



            //获取上传图片的输入流
            InputStream inputStream = file.getInputStream();
            PutObjectArgs build = PutObjectArgs.builder()
                    //上传对象是哪一个桶
                    .bucket(minioProperties.getBucketName())
                    //上传的对象在Minio存储的名字
                    .object(newFileName)
                    //上传对象的流
                    .stream(inputStream, inputStream.available(), -1)
                    .build();
            //上传图片
            minioClient.putObject(build);
            String url =minioProperties.getEndpoint()+"/"+minioProperties.getBucketName()+"/"+newFileName;
            return url;

        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }

        return null;
}
}
